package gui.naologado;

import conexao.Http;
import conexao.HttpData;
import conexao.HttpListener;
import java.io.DataInputStream;
import javax.microedition.lcdui.*;
import recados.Recados;

public class Login extends Form implements CommandListener, HttpListener {

    private final Recados midlet;
    public final TextField campoNome;
    public final TextField campoSenha;
    public final ChoiceGroup cg;
    private final Command cmdSair;
    private final Command cmdEntrar;
    private final Command cmdMenu;

    public Login(Recados midlet) {
        super("Recados");

        this.midlet = midlet;

        cmdSair = new Command("Sair", Command.EXIT, 0);
        cmdEntrar = new Command("Entrar", Command.OK, 1);
        cmdMenu = new Command("Menu", Command.SCREEN, 2);

        campoNome = new TextField("Nome:", null, 20, TextField.ANY);
        campoSenha = new TextField("Senha:", null, 20, TextField.PASSWORD);

        cg = new ChoiceGroup(null, Choice.MULTIPLE);
        cg.append("Lembrar", null);

        append(campoNome);
        append(campoSenha);
        append(cg);

        addCommand(cmdSair);
        addCommand(cmdEntrar);
        addCommand(cmdMenu);

        setCommandListener(this);
    }

    public void commandAction(Command c, Displayable d) {
        if (c == cmdSair) {
            midlet.destroyApp(true);
        } else if (c == cmdEntrar) {
            midlet.getLoginConfig().setNome(campoNome.getString());
            midlet.getLoginConfig().setSenha(campoSenha.getString());

            run();
        } else if (c == cmdMenu) {
            Recados.setCurrent(midlet.getLoginMenu());
        }
    }

    public void run() {
        Http http = new Http("Fazendo Login");
        HttpData dados = new HttpData();
        
        dados.setPagina("/login.php");

        dados.put("nome", campoNome.getString());
        dados.put("senha", campoSenha.getString());

        http.setHttpData(dados);
        
        http.setListener(this);

        Recados.setCurrent(http);

        http.doPost(false);
    }

    public void requisicaoCancelada() {
        Recados.setCurrent(this);
    }

    public void falhaRequisiçao(String mensagem) {
        Recados.setCurrent(new Alert("Erro", mensagem, null, AlertType.ERROR), this);
    }

    public void requisicaoConcluida(DataInputStream in) {
        String resposta = Http.readLine(in);

        if (resposta.equals("ok")) {
            boolean[] selecteds = new boolean[1];

            cg.getSelectedFlags(selecteds);

            if (selecteds[0]) {
                midlet.getLoginConfig().salva();
            }

            midlet.logged();

            Recados.setCurrent(midlet.getPrincipal());
        } else if (resposta.equals("erro")) {
            String segundaLinha = Http.readLine(in);
            Recados.setCurrent(new Alert("Erro", segundaLinha, null, AlertType.ERROR), midlet.getLogin());
        }
    }
}
